package ui.tp3.TP3Wicket;

import java.util.ArrayList;


import org.apache.wicket.Component;
import org.apache.wicket.Page;

import org.apache.wicket.request.resource.ContextRelativeResource;

import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.WindowClosedCallback;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ImageButton;

import org.apache.wicket.markup.html.list.ListItem;

import org.apache.wicket.markup.html.list.PropertyListView;

import org.apache.wicket.markup.html.image.Image;


import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.CompoundPropertyModel;



import ui.tp3.modelo.Personaje;
import ui.tp3.modelo.Tablero;


public class PersonajeStatusPage extends WebPage {
	private static final long serialVersionUID = 2L;
	final private Personaje personaje;
	String selected = "0";
	private Component panelPers;
	private Tablero tablero;
	private ModalWindow modal1;

      
    public PersonajeStatusPage(AccionesSobreElMapaPage accionesSobreElMapaPage) {
       
    	final AccionesSobreElMapaPage acp= accionesSobreElMapaPage;
      	this.personaje = accionesSobreElMapaPage.getPersonajeSelected();   
      	this.tablero = acp.getTablero();
    	
    	final WebMarkupContainer contenedorCreacionMapa= new WebMarkupContainer("divFormularioCreacionPersonaje");
    	contenedorCreacionMapa.setOutputMarkupId(true);
    	  	 
    	Form form = new Form("formulario1");
    	form.setOutputMarkupId(true);
    	
    	form.add(modal1 = new ModalWindow("modal1"));

        modal1.setTitle(personaje.getNombre() + " tiene las siguietes esferas ");
        modal1.setCookieName("modal-1");
    	
    	this.addPersCreados(form);
    	
    	final WebMarkupContainer contenedorMapa= new WebMarkupContainer("formulario1");
    	contenedorMapa.setOutputMarkupId(true);
    	
        form.add(new Button("volver"){
			private static final long serialVersionUID = 1L;
			
			public void onSubmit() {
				this.setResponsePage(acp);
				
			}
        	
        });

    	contenedorCreacionMapa.add(form);
    	add(contenedorCreacionMapa);
	
    }
    
    private void addPersCreados(final Form tablForm) {

		this.panelPers = new PropertyListView<Personaje>("personajes",
				PersonajeStatusPage.this.tablero.getPersonajes()) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Personaje> p) {
				final Personaje personaje = (Personaje) p.getModelObject();
				ImageButton imgP = new ImageButton("imgPers", new ContextRelativeResource("/"
						+ personaje.getNombre() + ".jpg"));
				imgP.add(new AjaxEventBehavior("onClick"){
					
					@Override
					protected void onEvent(AjaxRequestTarget target) {
						PersonajeStatusPage.this.crearModalEsf(personaje);
		        		modal1.show(target);
						
					}
					
				});
				p.add(imgP);
				p.add(new Label("nombre"));
				p.add(new Label("coordenada"));
				p.add(new Label("moveMax"));
				p.add(new Label("cantEsferasObtenidas"));

				p.add(new AjaxLink("esferasObtenidas") {

					@Override
					public void onClick(AjaxRequestTarget target) {
							PersonajeStatusPage.this.crearModalEsf(personaje);
			        		modal1.show(target);
					}
				});

			}
		};
		this.panelPers.setOutputMarkupId(true);
		tablForm.add(this.panelPers);

	}
    
    protected void crearModalEsf(final Personaje personaje2) {
    	modal1.setPageCreator(new ModalWindow.PageCreator(){
    	       
			private static final long serialVersionUID = 1L;

			public Page createPage(){
				return new EsferasPersonajeModalWindow(personaje2, modal1);
			}
        });
    	/*modal1.setWindowClosedCallback(new WindowClosedCallback(){
    		private static final long serialVersionUID = 1L;

			public void onClose(AjaxRequestTarget target) {
				target.addComponent(PersonajeStatusPage.this);

				
			}

    	
    		
    		
    	});*/

		
	}

	public Personaje getPersonaje(){
    	return this.personaje;
    }
    
    
    public String getSelected()
    { return this.selected;}

    public void setSelected(String s)
    {  this.selected=s;}
    
}

